import os

import yaml

class ApplicationConfig:
    # 获取当前脚本的目录
    base_dir = os.path.dirname(os.path.abspath(__file__))
    grandparent_dir_resources = os.path.join(base_dir, os.path.pardir, os.path.pardir, 'resources')
    # 构建完整的配置文件路径
    config_file = os.path.join(grandparent_dir_resources, 'application.yaml')
    def __init__(self, config_file = config_file):
        with open(config_file, 'r') as f:
            self.config = yaml.safe_load(f)

    def get_database_config(self):
        return self.config.get('database', {})

    def get_upload_config(self):
        return self.config.get('upload_dir', {})

    def get_root_dir(self):
        return self.config.get('root_dir', {})

    def get_minio(self):
        return self.config.get('minio', {})

    def write_root_dir_to_config(self,root_dir):
        # 将项目根目录路径写入配置文件
        self.config['root_dir'] = root_dir
        self.write_config_to_file()

    def write_config_to_file(self):
        # 将配置写入配置文件
        with open(self.config_file, 'w') as f:
            yaml.dump(self.config, f)
